package com.jhx.demos.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.jhx.demos.web.Utils.JwtUtils;
import com.jhx.demos.web.pojo.Admin;
import com.jhx.demos.web.pojo.Result;
import com.jhx.demos.web.pojo.User;
import com.jhx.demos.web.service.adminService;
import com.jhx.demos.web.service.userService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
public class loginController {

    @Autowired
    private userService userservice;

    @Autowired
    private adminService adminService;

    //员工注册
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        log.info("员工注册: {}",user);
        userservice.register(user);
        return Result.success("注册信息成功");
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){//将json格式数据封装到实体类中
        log.info("员工登录：{}",user);
        User u=userservice.login(user);//进行登录校验
//        //登陆成功，生成令牌，下发令牌
//        if (u != null) {
//            Map<String, Object> claims = new HashMap<>();
//            claims.put("id", u.getId());
//            claims.put("name", u.getUsername());
//            claims.put("username", u.getUsername());//将当前登录的员工信息封装到Map集合中
//
//            String jwt = JwtUtils.generateJwt(claims);//将集合传递进去
//            return Result.success(jwt);//登陆成功下发jwt令牌
//
//        }
        if (u!= null){
            return Result.success(user);
        }
        //登陆失败，返回错误信息
        return Result.error("用户名或者密码错误");
    }

    @PostMapping("/login1")
    public Result login(@RequestBody Admin admin){//将json格式数据封装到实体类中
        log.info("管理员登录：{}",admin);
        Admin a=adminService.login1(admin);//进行登录校验
        return a!=null?Result.success(a):Result.error("用户名或密码错误");//判断是否为Null,是则登陆失败
    }

    @PostMapping("/logout")
    public Result logout() {
        log.info("用户退出登录");
        return Result.success("退出成功");
    }
}
